package cn.shenlong.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Channel;
import cn.shenlong.portal.domain.Content;
import cn.shenlong.portal.domain.ContentChannel;
import cn.shenlong.portal.mapper.ChannelMapper;
import cn.shenlong.portal.mapper.ContentChannelMapper;
import cn.shenlong.portal.mapper.ContentMapper;
import cn.shenlong.portal.service.ContentService;
import cn.shenlong.util.StrUtils;
import cn.shenlong.util.UploadUtil;
import cn.shenlong.web.ThreadVariable;

@Service("contentService")
public class ContentServiceImpl implements ContentService {

	@Autowired
	private ContentMapper contentMapper;
	@Autowired
	private ChannelMapper channelMapper;

	@Autowired
	private ContentChannelMapper contentChannelMapper;

	@Override
	@Transactional
	public void save(Content content, long channelId) {
		contentMapper.saveEntity(content);
		ContentChannel cc = new ContentChannel();
		cc.setChannelId(channelId);
		cc.setContentId(((Content) findContents(content, null, null, null)
				.getList().get(0)).getContentId());
		contentChannelMapper.saveEntity(cc);
	}

	@Override
	public Pagination findContents(Content content,
			Map<String, Object> otherParm, Integer pageSize, Integer pageNo) {
		Pagination p = new Pagination();
		List<Content> contentList = contentMapper.findPageListByCriteria(
				initContentParm(content, otherParm),
				initRowBounds(pageSize, pageNo, p));
		Long count = contentMapper.findCountByCriteria(initContentParm(content,
				otherParm));
		p.setList(contentList);
		p.setTotalCount(count.intValue());
		return p;
	}

	private Map<String, Object> initContentParm(Content content,
			Map<String, Object> otherParm) {
		Map<String, Object> parm = new HashMap<String, Object>();
		if (otherParm != null) {
			parm.putAll(otherParm);
		}
		if (content == null) {
			return parm;
		} else {
			parm.put("contentId", content.getContentId());
			parm.put("contentType", content.getContentType());
			parm.put("title", StrUtils.replaceBlank(content.getTitle()));
			parm.put("txt", content.getTxt());
			parm.put("createDate", content.getCreateDate());
			parm.put("updateDate", content.getUpdateDate());
			parm.put("priority", content.getPriority());
			parm.put("typeImage", content.getTypeImage());
			parm.put("titleImage", content.getTitleImage());
			parm.put("link", content.getLink());
			parm.put("hasLink", content.getHasLink());
			return parm;
		}

	}

	private RowBounds initRowBounds(Integer pageSize, Integer pageNo,
			Pagination p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}

	@Override
	public Content findContentByContentId(long contentId) {
		return contentMapper.findByPK(contentId);

	}

	@Override
	public Content updateContent(Content content) {
		contentMapper.updateEntity(content);
		return contentMapper.findByPK(content.getContentId());
	}

	@Override
	@Transactional
	public void deleteContent(Long[] contentId) {
		if (contentId == null) {
			return;
		}
		for (int i = 0; i < contentId.length; i++) {
			Long id = contentId[i];
			Content content = contentMapper.findByPK(id);
			if (content != null) {
				UploadUtil.deleteImgForHtml(content.getTxt());
				UploadUtil.deleteForUrl(content.getTitleImage());
				UploadUtil.deleteForUrl(content.getTypeImage());
			}
		}

		contentMapper.deleteByPKArray(contentId);

	}

	@Override
	@Transactional
	public void deleteContentByChannelId(long channelId) {
		List<Channel> channelList = channelMapper
				.getChildListByChannelId(channelId);
		List<ContentChannel> ccList = new ArrayList<ContentChannel>();

		for (Channel channel : channelList) {
			ContentChannel cc = contentChannelMapper.findByChannelId(channel
					.getChannelId());
			if (cc != null) {
				ccList.add(cc);
			}

		}
		// 先删除关联关系
		for (Channel channel : channelList) {
			contentChannelMapper.deleteByChannelId(channel.getChannelId());
		}
		// 在删除实际数据
		for (ContentChannel contentChannel : ccList) {
			// 查看是否还有 栏目引用到
			Long count = contentChannelMapper
					.findCountByContentId(contentChannel.getContentId());
			if (count == 0) {// 不存在引用，删除内容数据
				deleteContent(new Long[] { contentChannel.getContentId() });
			}

		}

	}

	@Override
	@Transactional
	public void deleteByContentIdAndChannelId(List<Long> contentIds,
			long channelId) {
		List<Channel> channelList = channelMapper
				.getChildListByChannelId(channelId);
		for (Long contentId : contentIds) {

			for (Channel channel : channelList) {
				contentChannelMapper.deleteByContentIdAndChannelId(contentId,
						channel.getChannelId());
			}

			// 删除后 查看是否还有 栏目引用到
			Long count = contentChannelMapper.findCountByContentId(contentId);
			if (count == 0) {// 不存在引用，删除内容数据
				deleteContent(new Long[] { contentId });
			}
		}

	}
}
